import { ref, computed } from "vue";
// 封装购物车

import { defineStore } from "pinia";
//用戶数据
import { useUserStore } from "@/stores/userStore.js";
import { insertCartAPI, getCartListApi, delCartApi } from "@/apis/cart";

export const useCartStore = defineStore(
    "cart",
    () => {
        // state
        const cartList = ref([]);
        const userStore = useUserStore();
        const isLogin = computed(() => userStore.userInfo.token);

        // action
        // 添加购物车
        const addCart = async (goods) => {
            // 登陆之后
            if (isLogin.value) {
                await insertCartAPI(goods.skuId, goods.count);
                updateCartList();
            }
            //未登陆
            else {
                // 使用find函数匹配购物车列表中是否存在skuid相同的数据
                const item = cartList.value.find((item) => goods.skuId === item.skuId);
                if (item) {
                    item.count = item.count + goods.count;
                } else {
                    cartList.value.push(goods);
                }
            }
        };

        // 删除购物车
        const delCart = async (skuId) => {
            //登陆删除
            if (isLogin.value) {
                await delCartApi([skuId]);
                updateCartList();
            } else {
                //未登录删除
                // 传递skuid，匹配相同的skuID拿到下标
                const index = cartList.value.findIndex((item) => skuId === item.skuId);
                // 删除对应索引的数据
                cartList.value.splice(index, 1);
            }
        };

        // 获取总价
        const getAllCount = computed(() => {
            let sum = 0;
            cartList.value.forEach((item) => {
                sum = sum + item.count;
            });
            return sum;
        });

        // 获取总数量
        const getTatial = computed(() => {
            let sum = 0;
            cartList.value.forEach((item) => {
                sum = sum + Number(item.price);
            });
            return sum.toFixed(2);
        });

        // 单选功能
        const changSelect = (skuId, selected) => {
            const item = cartList.value.find((item) => item.skuId === skuId);
            item.selected = selected;
        };

        // 是否全选
        const isAll = computed(() => cartList.value.every((item) => item.selected));

        // 全选
        const allSelect = (selected) => {
            cartList.value.forEach((item) => (item.selected = selected));
        };

        // 已选择总价
        const selectSum = computed(() => {
            const selectGoods = cartList.value.filter((item) => item.selected);
            let sum = 0;
            selectGoods.forEach((item) => {
                sum = sum + Number(item.count) * Number(item.price);
            });
            return sum;
        });

        // 已选择总数
        const selectCount = computed(() => {
            const selectGoods = cartList.value.filter((item) => item.selected);
            let count = 0;
            selectGoods.forEach((item) => {
                count = count + Number(item.count);
            });
            return count;
        });

        // 更新购物车列表
        const updateCartList = async () => {
            const res = await getCartListApi();
            cartList.value = res.result;
        }


        // 清空购物车
        const clearCart = () => { 
            cartList.value = []
        }

        return {
            cartList,
            getAllCount,
            getTatial,
            isAll,
            selectSum,
            selectCount,
            addCart,
            delCart,
            changSelect,
            allSelect,
            clearCart,
            updateCartList,
        };
    },
    {
        persist: true,
    }
);
